"""
@author: jpj
@email: pj.jiang@hzgosun.com
@file: ondutyQueryTest.py
@time: 2019/7/24 11:48

"""
import os
import unittest
from time import sleep

from selenium.webdriver.common.keys import Keys

from common.decorators import log4gs, mkdir_classname
from common.functions import save_screenshot
from common.settings import TAG, DATA_DIR
from pages.ondutyQueryPage import Ondutyquery


@log4gs
@mkdir_classname(TAG)
class OndutyQueryTest(unittest.TestCase):
    """夜间值班保安功能测试"""

    @classmethod
    def setUpClass(cls):
        cls.log.info('=' * 30 + "开始夜间值班保安功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page = Ondutyquery('/DataManage/OndutyQuery')

    @classmethod
    def tearDownClass(cls):
        cls.log.info('=' * 30 + "结束夜间值班保安功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page.driver.quit()

    def setUp(self):
        self.log.info('>>>>>开始执行用例：{} {}'.format(self.id(), self.shortDescription()))

    def tearDown(self):
        self.log.info('<<<<<结束执行用例：{} {}'.format(self.id(), self.shortDescription()))
        sleep(2)

    def test_1_page(self):
        """值班表保安翻页"""
        sleep(1)
        self.log.info("共有：{}页数据".format(self.page.turnPage.total_page))
        self.log.info("当前处于第：{}页数据".format(self.page.turnPage.current_page))
        self.log.info("共有：{}条数据".format(self.page.turnPage.total_num))
        sleep(2)
        self.assertNotEqual(self.page.turnPage.total_page, 1, msg="页数过少，无法进行翻页测试")
        if int(self.page.turnPage.total_page) >= 3:
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')
            sleep(2)
            self.log.info("点击上一页当前处于第：{}页数据".format(self.page.turnPage.current_page))
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '3')
            sleep(2)
            self.log.info("点击上一页当前处于第：{}页数据".format(self.page.turnPage.current_page))
            self.page.turnPage.prev_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')
            sleep(2)
            self.log.info("点击上一页当前处于第：{}页数据".format(self.page.turnPage.current_page))
            self.page.turnPage.input.clear()
            self.page.turnPage.input.send_keys(3)
            self.page.turnPage.input.send_keys(Keys.ENTER)
            self.assertEqual(self.page.turnPage.current_page, '3')
            self.log.info("输入3后当前处于第：{}页数据".format(self.page.turnPage.current_page))
        else:
            self.log.error("当前模块最大未到3页，无法进行翻页测试", exc_info=True)
            save_screenshot(self, '翻页功能测试失败')

    def test_2_download(self):
        """下载模板"""
        self.page.searchTools.download.click()

    def test_3_upload(self):
        """上传值班表"""
        name = "郑锻西苑"
        sleep(1)
        try:
            self.page.searchTools.upload.send_keys(os.path.join(DATA_DIR, "值班保安.xlsx"))
        except:
            self.log.error("保安文件错误", exc_info=True)
        sleep(2)
        result = self.page.table.get_row(name)
        try:
            self.assertTrue(result, f"找到{name}")
            self.log.info("用例导入表格执行成功")
        except Exception:
            self.log.error("用例导入表格执行失败,", exc_info=True)

    def test_4_search(self):
        """日期查询验证"""
        name = '二七区'
        time_init = str(self.page.searchTools.time_input.get_attribute('value')).split("-")
        present_time = "2017-07-12".split("-")
        if time_init[0] == present_time[0]:
            if time_init[1] == present_time[1]:
                self.page.searchTools.time_input.click()
                sleep(1)
                self.page.searchTools.time.click()
            else:
                time_b = float('.'.join(time_init[1])) * 10 - float('.'.join(present_time[1])) * 10
                self.page.searchTools.time_input.click()
                sleep(1)
                for i in range(int(str(time_b).split('.')[0])):
                    self.page.searchTools.select_time.click()
                sleep(1)
                self.page.searchTools.time.click()
        else:
            num = int(time_init[0]) - int(present_time[0])
            time_b = (num * 12 + float('.'.join(time_init[1])) * 10) - float('.'.join(present_time[1])) * 10
            self.page.searchTools.time_input.click()
            sleep(1)
            for i in range(int(str(time_b).split('.')[0])):
                self.page.searchTools.select_time.click()
            sleep(1)
            self.page.searchTools.time.click()
        sleep(1)
        result = self.page.table.get_row(name)
        try:
            self.assertTrue(result, f"找到{name}")
            self.log.info("用例日期7月12号查询执行成功".format(self.id()))
        except Exception:
            self.log.error("用例日期7月12号查询执行失败,".format(self.id()), exc_info=True)


if __name__ == '__main__':
    unittest.main()